package net.core.base.struts.result;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;

public class XMLResult  implements Result {
	private static final long serialVersionUID = 1L;
	private static final String XML_CHARSET="UTF-8";
	private StringBuffer xmlbuffer;
	boolean xmlSet = false;
	public void execute(ActionInvocation invocation) throws Exception {
		StringBuffer xmlbuffer = null;

		if (xmlSet) {
			xmlbuffer = this.xmlbuffer;
		} else {
			xmlbuffer = (StringBuffer) invocation.getStack().findValue("xmlbuffer");
		}

		if (xmlbuffer == null) {
			throw new NullPointerException("No XML found");
		}
		// 把文件流换成是通过HttpServletResponse对象获取到的输出流
		
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml; charset=" + XML_CHARSET); 
		response.setHeader("Cache-Control", "no-cache");
		response.setHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT");
		PrintWriter out = response.getWriter(); 
		out.println(xmlbuffer.toString()); 
		out.flush(); 

		
	}
	public StringBuffer getXmlbuffer() {
		return xmlbuffer;
	}
	public void setXmlbuffer(StringBuffer xmlbuffer) {
		this.xmlbuffer = xmlbuffer;
	}

}
